using System;

namespace ApplicationSupport
{
	/// <summary>
	/// Safe parsing of objects
	/// </summary>
	public static class SafeConvert
	{
        /// <summary>
        /// Converts the specified object to it's Boolean equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static bool ToBoolean(object value)
        {
            try
            {
                return value != null ? Convert.ToBoolean(value) : false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Converts the specified object to it's Int16 equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static short ToInt16(object value)
        {
            try
            {
                return value != null ? Convert.ToInt16(value) : (short)0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts the specified object to it's Int32 equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static int ToInt32(object value)
        {
            try
            {
                return value != null ? Convert.ToInt32(value) : 0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts the specified object to its Int64 equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static long ToInt64(object value)
        {
            try
            {
                return value != null ? Convert.ToInt64(value) : 0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts the specified object to it's Byte equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static byte ToByte(object value)
        {
            try
            {
                return value != null ? Convert.ToByte(value) : (byte)0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts the specified object to it's Single equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static float ToFloat(object value)
        {
            try
            {
                return value != null ? Convert.ToSingle(value) : 0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts the specified object to it's Double equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static double ToDouble(object value)
        {
            try
            {
                return value != null ? Convert.ToDouble(value) : 0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts the specified object to it's Decimal equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static decimal ToDecimal(object value)
        {
            try
            {
                return value != null ? Convert.ToDecimal(value) : 0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts the specified object to it's DateTime equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static DateTime ToDateTime(object value)
        {
            try
            {
                return value != null ? Convert.ToDateTime(value) : DateTime.MinValue;
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// Converts the specified object to it's String equivalent
        /// </summary>
        /// <param name="value">A object to convert</param>
        public static string ToString(object value)
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                return value.ToString();
            }
        }
	}
}